package org.mushroomdb.transaction;

import java.util.TimerTask;

import org.mushroomdb.service.ServiceManager;

/**
 * @author Tomas
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TrxTimerTask extends TimerTask{
    private Transaction trx;
    private Thread thread;
    private boolean started = false;
    private boolean skip= false;
   
    public TrxTimerTask(Transaction trx, Thread thread){
        this.trx = trx;
        this.thread = thread;
        
    }
    
    public void run()  {
        if(this.started){
            if(!this.skip){
            this.trx.rollback();
            TransactionService service = (TransactionService) ServiceManager.getInstance().getService(TransactionService.class);
            service.removeTransaction(this.thread);
            //thread.stop();
            throw new TimeOutException("La transaccion ha tomado mucho tiempo");
            }
            this.skip=false;
        }
    }
    
    public void start(){
        this.started=true;
    }
    
    public void skip(){
        this.skip=true;
    }

}
